<?php
exit;
// card id  pe2K2jueSlpaafyz1PB30pXpeInk
ini_set('display_errors', 'On');
define('APPID', 'wx96d3d51340c0ee8d');
define('SECRET', '06308b7ab25808e376191dd9ff0bd5c3');
 
function get_access_token(){
	$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".SECRET;
	$res = https_request($url, 'get', 'json');
	$access_token = $res["access_token"];
	return $access_token;
}
 
function https_request($url, $type="get", $res="json", $data = ''){
    //1.初始化curl
    $curl = curl_init();
    //2.设置curl的参数
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    if ($type == "post"){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    //3.采集
    $output = curl_exec($curl);
    //var_dump(curl_getinfo($curl));
    //4.关闭
    curl_close($curl);
    if ($res == 'json') {
        return json_decode($output,true);
    }
}
 
//上传图片素材(该方法只能获取到图片的URL)     2016-11-3
function addMaterialUrl() {
	$access_token = get_access_token();    
	$url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=".$access_token;
	$filename = 'test.png';  //文件名同目录下 其他目录自己指定
	$data = array("media"=>'@'. $filename);
	$res = https_request( $url ,'post', 'json', $data);
	print_r($res);
	return $res['url'];
}
 
function createCardMeg ($logo_url) {
	$access_token = get_access_token();    
	$url = "https://api.weixin.qq.com/card/create?access_token=".$access_token;
	$data = '{
  "card": {
      "card_type": "GENERAL_COUPON",
      "general_coupon": {
          "base_info": {
              "logo_url":"'.$logo_url.'",
              "brand_name": "大娘水饺",
              "code_type": "CODE_TYPE_TEXT",
              "title": "酸菜鱼汤饺立减2元",
              "color": "Color070",
              "notice": "向店员出示",
              "description": "1、本券仅限在大娘水饺指定餐厅使用，具体情况以餐厅公示为准；
2、凭本券购买酸菜鱼汤饺10只立减2元，核销后自动失效；
3、本券不与其他优惠券及优惠活动同享；
4、本券仅限到店使用，不适用外送、线上预点餐、小程序点餐等服务；
5、本券不兑换现金，复制、转发、截图无效；
6、如遇酸菜鱼汤饺售罄，凭此券购买荠菜猪肉汤饺1份可立减2元；
7、本券有效期至2020年2月23日，逾期作废。",
              "date_info": {
                  "type": "DATE_TYPE_FIX_TIME_RANGE",
                  "begin_timestamp": 1578223785,
                  "end_timestamp": 1579784697
              },
              "sku": {
                  "quantity": 10000000
              },
              "use_limit":900,
              "get_limit": 900,
              "use_custom_code": true,
              "bind_openid": false,
              "can_share": true,
              "can_give_friend": true 
          },
           "advanced_info": {
               "abstract": {
                   "abstract": "鱼见金喜 红运鼠你",
                   "icon_url_list": [
                       "http://mmbiz.qpic.cn/mmbiz_png/OaI11CibiaVfEBEv6E2HFTbTicHW7pHJUiasf5I5OFFxT1hA0je9nq7zGey5VISXquXwl6qzFt4vABevfiabYHk0dww/0?wx_fmt=png"
                   ]
               }
           },
           "default_detail":"凭本券到店购买酸菜鱼汤饺10只立减2元"
          
      }
  }}';
	$res = https_request( $url ,'post', 'json', $data);
	var_dump($res);
	return $res['card_id'];
}
 
function getQRTicket($card_id){
	$access_token = get_access_token();
	$data = ' {
		"action_name": "QR_CARD",
		"expire_seconds": 1800,
		"action_info": {
			"card": {
				"card_id": "'.$card_id.'",
				"code": "1234567890",
				"openid": "",
				"is_unique_code": false ,
				"outer_str":""
			}
		}
	}';
	$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token;
	$res = https_request( $url ,'post', 'json', $data);
	
    echo "<img src='https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket={$res['ticket']}' />";
	print_r($res);exit();
}
 
function wxCardWhiteList() {
	$access_token = get_access_token();
	$url = "https://api.weixin.qq.com/card/testwhitelist/set?access_token=".$access_token;
	$data = '{
		"openid": [
			"oG7Sh1R_j1bAmj_yz3jsAK5-Uep4"
		]
	}';
	$res = https_request( $url ,'post', 'json', $data);
	print_r($res);
}
 
 
//$logo_url = addMaterialUrl();
$logo_url='https://mmbiz.qlogo.cn/mmbiz_jpg/OaI11CibiaVfGLjJnQMy82VE2uPadFNKEA3mmwRMDjoutZRZZskbC2vNica5ljkaUPHnIVGLCbxKQ1QcFH2NvPU0w/0?wx_fmt=jpeg';
$card_id = createCardMeg($logo_url);
//wxCardWhiteList();
//getQRTicket($card_id);
 